import { CustomEnum } from 'custom/index'
import AuthButton from './AuthButton'
import { ElMessage, ElMessageBox } from 'element-plus'

export default class AuthButtonInTable extends AuthButton {
  public static name: CustomEnum = 'AuthButtonInTable';

  protected static props = {
    index: {
      type: Number,
      default: 0
    },
    value: null,
    row: {
      type: Object,
      default: () => ({})
    }
  }

  toDetail(name: string, getTo: (row: any, ctx: any) => any = (row) => ({ params: { id: row.id } })) {
    return this.action((ctx) => {
      const row = ctx.props.row || {}
      const router = ctx.router
      router.push(Object.assign({ name }, getTo(row, ctx)))
    })
  }

  removeRow(api: (ids: any) => Promise<any>, getIds = row => [row.id]) {
    this.action(async ({ props, _c }) => {
      await ElMessageBox.confirm('是否确定删除？', '提示')
      const row = props.row || {}
      const ids = getIds(row)
      if (!ids.map(i => i).length) return
      await api(ids)
      ElMessage.success('操作成功')
      _c.dispatch('ListPage', 'getData')
    })
    return this
  }

  end() {
    return this.upto('AuthButtonColumn')
  }
}
